
package view;

import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public abstract class CustomizedStage extends Stage{

    private String barButtonsClass="bar-buttons";
    
    public static final int HIDE=1;
    public static final int EXIT=2;
    public static final int NOTHING=3;

    private double initX;
    private double initY;
    
    private GridPane root;
    private Scene scene;
    private HBox titleBar;
    private Label titleL;
    private Button closeB;
    private Button minimizeB;
    
    public CustomizedStage(Stage owner, String title, double width, double height) {
        super();
        initOwner(owner);
        initModality(Modality.APPLICATION_MODAL);
        setTitle(title);
        root=new GridPane();
        root.setId("my-root");
        if(width<=0 || height<=0)
            scene=new Scene(root);
        else
            scene=new Scene(root, width, height);
        scene.setFill(Color.TRANSPARENT);
        scene.getStylesheets().add("view/styles.css");
        
        titleBar=new HBox(0);
        titleBar.setPrefWidth(width);
        titleBar.setId("title-bar");
        closeB=new Button("x");
        minimizeB=new Button("-");
        closeB.getStyleClass().add(barButtonsClass);
        minimizeB.getStyleClass().add(barButtonsClass);
        titleL=new Label(title);
        
        titleBar.setOnMousePressed(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent me) {
                initX=me.getScreenX()-getX();
                initY=me.getScreenY()-getY();
            }
        });
        titleBar.setOnMouseDragged(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent me) {
                setX(me.getScreenX()-initX);
                setY(me.getScreenY()-initY);
            }
        });
        minimizeB.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent arg0) {
                setIconified(true);
            }
        });
        setOnClose(EXIT);
        titleBar.getChildren().setAll(titleL,minimizeB,closeB);
        GridPane.setConstraints(titleBar, 0, 0);
        root.add(titleBar,0,0);
        initStyle(StageStyle.TRANSPARENT);
        setResizable(false);
        setScene(scene);
    }
    
    protected final void setContent(final Pane content){
        root.add(content,0,1);
    }
    
    protected final void setOnClose(int option){
        switch(option){
            case HIDE:
                closeB.setOnAction(new EventHandler<ActionEvent>() {
                    @Override
                    public void handle(ActionEvent arg0) {
                        close();
                    }
                });
                break;
            case EXIT:
                closeB.setOnAction(new EventHandler<ActionEvent>() {
                    @Override
                    public void handle(ActionEvent arg0) {
                        Platform.exit();
                        System.exit(0);
                    }
                });
                break;
            case NOTHING:
                closeB.setOnAction(null);
                break;
        }
    }
    
    protected final void setOnClose(EventHandler<ActionEvent> eventHandler){
        closeB.setOnAction(eventHandler);
    }
    
}